import os, ConfigParser, time, shutil

def GetConfig(inifile):
    """
    Function to parse the ini file.
    Accepts : A path to an ini text file.
    Returns : True / False 
    """
    #start the main code here

    global config

    config = ConfigParser.ConfigParser()#Create our ConfigParser object here

    if os.path.exists(inifile):                #does the file exist?
        try:
            config.read(inifile)            #if we can open our ini file we can continue to read in the values
            return True
   
        except ConfigParser.MissingSectionHeaderError, ConfigParser.ParsingError :
            return False
    else:
        return False

def GetSetting(section, option, retdefault=None, type='s'):
    """
    Function to return the value from the requested INI file options.
    Accepts : Section ([Section]), option (setting:), type ('s' or 'i')
    Returns : A value from the chosen option or false if an error occurs.  
    """

    try:
        #read our INI file for common settings
        if type=='s':                                   #This is a string, our default type
            return config.get(section, option)          #Get is the default method and is used to return strings.

        else:                                           #else, for the purposes of this code, it is an integer.
            return config.getint(section, option)       #I have used GetInt here because I specifically want this valye returned as an int.

    except ConfigParser.NoSectionError, x:
        if retdefault!=None:
            return retdefault
        else:
            return False

    except ConfigParser.NoOptionError, x:
        if retdefault!=None:
            return retdefault
        else:
            return False
        
def FindMovieImage(MoviePath, Type):
    if Type=="F":
        ext1 = "fanart.jpg"
        ext2 = "fanart.png"
    else:
        ext1 = "jpg"
        ext2 = "png"     
    fanart = MoviePath[:-3] + ext1 
    if os.path.exists(fanart):
         return fanart
    fanart = MoviePath[:-3] + ext2 
    if os.path.exists(fanart):
         return fanart 
    return None
        
def FindCover(MoviePath):
    cover = MoviePath[:-3] + "jpg" 
    if os.path.exists(cover):
         return cover
    fanart = MoviePath[:-3] + "png" 
    if os.path.exists(cover):
         return cover 
    return ""    

def LogEvents(Mode, TheClass):
    global LogFile
    LogFile = TheClass.LogFile
    TheClass.DoLog = True
    TheClass.LogMode = Mode 
    #TheClass.LogFile = LogFile

def LogEvent(EventText):         
    #if TheClass.DoLog == True:
    try:
        EventLog = open(LogFile, "a")
        EventLog.write(time.strftime('[%H:%M:%S] ', time.localtime()))
        EventLog.write(unicode(EventText)+ "\n") 
        EventLog.close()
    except Exception ,x:
        print x
        try:
            EventLog.write(EventText + "\n")
            EventLog.close()
        except Exception ,x:
            print x    
    #finally:
        #EventLog.close()
        
def MoveFile(filename, location):
    """
    Function to move a page text file to either the error or log folder
    Accepts : filename ('Pagefile.txt'), location ('C:\BOPDHBCode\Python\Paging\'), create (True or False)
    Returns : True / False
    """
    if os.path.exists(filename):
        try:
           shutil.move(filename, location)                      #Does not return true (or presumably false)
           return True

        except Exception, x:
            print x
            return False